package cn.exrick.xboot.modules.AccessControl.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.utils.SnowFlakeUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.AccessControl.AccessControlUserDto.AcForm;
import cn.exrick.xboot.modules.AccessControl.AccessControlUserDto.AcUserForm;
import cn.exrick.xboot.modules.AccessControl.AccessControlUserDto.Acdto;
import cn.exrick.xboot.modules.AccessControl.AccessControlUserDto.UserDto;
import cn.exrick.xboot.modules.AccessControl.dao.mapper.AccessControlUserMapper;
import cn.exrick.xboot.modules.AccessControl.entity.AccessControlUser;
import cn.exrick.xboot.modules.AccessControl.service.IAccessControlUserService;
import cn.exrick.xboot.modules.base.entity.User;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author portal
 */
@Slf4j
@RestController
@Api(description = "AccessControlUser管理接口")
@RequestMapping("/xboot/accessControlUser")
@Transactional
public class AccessControlUserController {

    @Autowired
    private IAccessControlUserService iAccessControlUserService;
    @Autowired
    private AccessControlUserMapper accessControlUserMapper;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<AccessControlUser> get(@PathVariable String id){

        AccessControlUser accessControlUser = iAccessControlUserService.getById(id);
        return new ResultUtil<AccessControlUser>().setData(accessControlUser);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<AccessControlUser>> getAll(){

        List<AccessControlUser> list = iAccessControlUserService.list();
        return new ResultUtil<List<AccessControlUser>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<AccessControlUser>> getByPage(@ModelAttribute PageVo page){

        IPage<AccessControlUser> data = iAccessControlUserService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<AccessControlUser>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<AccessControlUser> saveOrUpdate(@ModelAttribute AccessControlUser accessControlUser){

        if(iAccessControlUserService.saveOrUpdate(accessControlUser)){
            return new ResultUtil<AccessControlUser>().setData(accessControlUser);
        }
        return new ResultUtil<AccessControlUser>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids){

        for(String id : ids){
            iAccessControlUserService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }

    @RequestMapping(value = "/addAcUser", method = RequestMethod.POST)
    @ApiOperation(value = "批量添加网络权限限制名单")
    public Result<Object> addAcUser(@RequestParam String ids){
        System.out.println("addAcUser:"+ids);

        return null;
    }

    @RequestMapping(value = "/getUserByAc", method = RequestMethod.GET)
    @ApiOperation(value = "njp获取应用服务没有限制网络的名单")
    public Result<IPage<UserDto>> getUserByAc(@ModelAttribute AcUserForm user,
                                             @ModelAttribute PageVo page){
        log.info("start getUserByAc ,dateTime:"+new Date());
        IPage<UserDto> userByAccessControl = iAccessControlUserService.findUserByAccessControl(PageUtil.initMpPage(page), user);
        log.info("end getUserByAc ,dateTime:"+new Date() +"listSize:"+userByAccessControl.getSize());
        return new ResultUtil<IPage<UserDto>>().setData(userByAccessControl);
    }

    @RequestMapping(value = "/getAc", method = RequestMethod.GET)
    @ApiOperation(value = "njp获取应用服务被限制网络的名单")
    public Result<IPage<Acdto>> getAc(@ModelAttribute AcUserForm user,
                                              @ModelAttribute PageVo page){
        log.info("start getAc ,dateTime:"+new Date());
        IPage<Acdto> userByAccessControl = iAccessControlUserService.findAccessControl(PageUtil.initMpPage(page), user);
        log.info("userByAccessControl:查询出"+userByAccessControl.getTotal()+"条");
        return new ResultUtil<IPage<Acdto>>().setData(userByAccessControl);
    }

    @RequestMapping(value = "/addAcUserBySys", method = RequestMethod.POST)
    @ApiOperation(value = "批量添加数据网络权限名单")
    public Result<Object> addAcUserBySys(@ModelAttribute AcForm acForm){
        String[] split = acForm.getUsers().split(",");
        log.info("seize:"+split.length);
        List<AccessControlUser> acuList=new ArrayList<>();
        for (String s : split) {
            AccessControlUser accessControlUser = new AccessControlUser(acForm.getSysId());
            accessControlUser.setUsername(s);
            acuList.add(accessControlUser);
        }
        log.info("listsize:"+acuList.size());
        boolean b = iAccessControlUserService.saveBatch(acuList);
        log.info("批量添加结果："+b);
        if(b) {
            return new ResultUtil<Object>().setSuccessMsg("批量添加成功");
        }
        return new ResultUtil<Object>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delAcByIds", method = RequestMethod.POST)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAcByIds( @ModelAttribute AcForm acForm){
        String[] ids = acForm.getUsers().split(",");
        for(String id : ids){
            iAccessControlUserService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }

}
